﻿using System;
using System.Collections;
using System.Collections.Generic;
using static OnlineSupermarket.OnlineSupermarket;

namespace OnlineSupermarket
{
    /// <summary>
    /// 表示一家线上超市。
    /// </summary>
    public sealed partial class OnlineSupermarket : ICollection<GoodsInfo>
    {
        private readonly List<GoodsInfo> goods;

        public OnlineSupermarket(params GoodsInfo[]? goods)
        {
            this.goods = goods == null ?
                new List<GoodsInfo>() :
                new List<GoodsInfo>(goods);
        }

        public int Count => this.goods.Count;
        bool ICollection<GoodsInfo>.IsReadOnly => false;

        public void Add(GoodsInfo goods)
        {
            if (goods == null)
                throw new ArgumentNullException(nameof(goods));
            this.goods.Add(goods);
        }

        public bool Remove(GoodsInfo goods)
        {
            if (goods == null)
                throw new ArgumentNullException(nameof(goods));
            return this.goods.Remove(goods);
        }

        public void Clear()
        {
            this.goods.Clear();
        }

        public bool Contains(GoodsInfo? goods)
        {
            if (goods == null)
                return false;
            return this.goods.Contains(goods);
        }

        void ICollection<GoodsInfo>.CopyTo(GoodsInfo[] array, int arrayIndex)
        {
            this.goods.CopyTo(array, arrayIndex);
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.goods.GetEnumerator();
        }

        IEnumerator<GoodsInfo> IEnumerable<GoodsInfo>.GetEnumerator()
        {
            return this.goods.GetEnumerator();
        }
    }
}
